home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <math.h>
- #define X1 0.46
- #define X2 0.54
- #define Y1 0.47
- #define Y2 0.53
- #define CHARGE1 0.1
- #define CHARGE2 -0.1
- #define SCALE .1
-
- main()
- {
- double x,y,z,u,v;
- double x1=X1,x2=X2, y1=Y1,y2=Y2, q1=CHARGE1,q2=CHARGE2;
- double dist1,dist2,E1x,E1y,E2x,E2y;
- double maguv,scale,ratio, rmin=1.0,rmax = 0.0;
- char title[81];
- char c;
- FILE *datfile;
-
- datfile=fopen("dipoleElectricField.data","w");
-
- for (z = 0.0; z < 0.201; z+=0.02){
- /* title line */
- fprintf(datfile,"z = %f, q1 = %f, q2 = %f\n", z,q1,q2);
- /* charge centers (with zero-length vectors) */
- fprintf(datfile,"%12.7f%12.7f%12.7f%12.7f%c\n", x1,y1,0.,0.,'r'); //red
- fprintf(datfile,"%12.7f%12.7f%12.7f%12.7f%c\n", x2,y2,0.,0.,'b'); //blue
- for (x = 0.1; x < .95; x+=0.2){
- for (y = 0.1; y < .95; y+=0.2){
- if (y > y1 && y < y2 && x > x1 && x < x2) {
- /* do nothing, to avoid large vectors between the charges */
- } else {
- dist1=sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1) + z*z);
- dist2=sqrt((x-x2)*(x-x2) + (y-y2)*(y-y2) + z*z);
- E1x = q1*(x-x1)/(dist1*dist1*dist1);
- E1y = q1*(y-y1)/(dist1*dist1*dist1);
- E2x = q2*(x-x2)/(dist2*dist2*dist2);
- E2y = q2*(y-y2)/(dist2*dist2*dist2);
- u=E1x+E2x;
- v=E1y+E2y;
- fprintf(datfile,"%12.7f%12.7f%12.7f%12.7f%c\n", x,y,u,v,'g');//green
- }
- }
- }
- fprintf(datfile,"%12.7f%12.7f%12.7f%12.7f%c\n", 0.,0.,0.,0.,'q');
- //separator between plots for different z's
- }
- fclose(datfile);
-
- /* Now read values back to find rmin and rmax to scale vectors */
-
- datfile=fopen("dipoleElectricField.data","r");
-
- for (z = 0.0; z < 0.201; z+=0.02){
- if (fgets(title, 81, datfile) == NULL) {
- printf("Done (or error in reading title)\n");
- break;
- }
- while (fscanf(datfile, "%lf%lf%lf%lf%c", &x,&y,&u,&v,&c)==5) {
- /* note that x...v are doubles, hence %lf in fscanf */
- //printf("x = %8.5f, y = %8.5f, u = %8.5f, v = %8.5f, c = %c\n",
- // x,y,u,v,c);
- if (c == 'q') {
- break;
- }
- if (u == 0.0 && v == 0.0) {
- //do nothing, zero-length vector, will be ignored
- } else {
- maguv = sqrt(u*u+v*v);
- if (maguv < rmin) rmin = maguv;
- if (maguv > rmax) rmax = maguv;
- }
- }
- printf("Exit block with c = %c, rmin = %f, rmax = %f\n", c,rmin,rmax);
- fscanf(datfile,"\n");
- } //end of for-loop on z
-
- printf("rmin = %f, rmax = %f\n", rmin,rmax);
- scale = SCALE*log(rmax/rmin);
- printf("scale = %f\n", scale);
- fclose(datfile);
-
- /* Now re-calculate and write scaled values back into data file */
-
- datfile=fopen("dipoleElectricField.data","w");
- for (z = 0.0; z < 0.201; z+=0.02){
- /* title line */
- fprintf(datfile,"z = %f, q1 = %f, q2 = %f\n", z,q1,q2);
- /* charge centers (with zero-length vectors) */
- fprintf(datfile,"%12.7f%12.7f%12.7f%12.7f%c\n", x1,y1,0.,0.,'r'); //red
- fprintf(datfile,"%12.7f%12.7f%12.7f%12.7f%c\n", x2,y2,0.,0.,'b'); //blue
- for (x = 0.1; x < .95; x+=0.2){
- for (y = 0.1; y < .95; y+=0.2){
- if (y > y1 && y < y2 && x > x1 && x < x2) {
- /* do nothing, to avoid large vectors between the charges */
- } else {
- dist1=sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1) + z*z);
- dist2=sqrt((x-x2)*(x-x2) + (y-y2)*(y-y2) + z*z);
- E1x = q1*(x-x1)/(dist1*dist1*dist1);
- E1y = q1*(y-y1)/(dist1*dist1*dist1);
- E2x = q2*(x-x2)/(dist2*dist2*dist2);
- E2y = q2*(y-y2)/(dist2*dist2*dist2);
- u = (E1x+E2x);
- v = (E1y+E2y);
- maguv = sqrt(u*u+v*v);
- ratio = SCALE*log(maguv/rmin)/log(rmax/rmin);
- u = u*ratio;
- v = v*ratio;
-
- fprintf(datfile,"%12.7f%12.7f%12.7f%12.7f%c\n", x,y,u,v,'g');//green
- }
- }
- }
- fprintf(datfile,"%12.7f%12.7f%12.7f%12.7f%c\n", 0.,0.,0.,0.,'q');
- //separator between plots for different z's
- }
- fclose(datfile);
- }
-